package com.befirefly.studentmanager.handler;

import com.befirefly.studentmanager.service.StudentService;
import com.befirefly.studentmanager.service.impl.StudentServiceImpl;
import com.befirefly.studentmanager.view.AddStudentView;
import com.befirefly.studentmanager.view.MainView;
import com.befirefly.studentmanager.view.UpdateStudentView;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author: 八千里路云和月
 * @Date: 2021/2/8 15:57
 * @Description:
 */
public class MainViewHandler implements ActionListener {

    private MainView mainView;

    public MainViewHandler(MainView mainView) {
        this.mainView = mainView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if (StringUtils.equals("增加", text)) {
            new AddStudentView(mainView);
        } else if (StringUtils.equals("修改", text)) {
            long[] selectedStudentIds = mainView.getSelectedStudentIds();
            if (ArrayUtils.isEmpty(selectedStudentIds)) {
                JOptionPane.showMessageDialog(mainView, "请选择要修改的数据！");
            } else if (selectedStudentIds.length > 1) {
                JOptionPane.showMessageDialog(mainView, "一次只能修改一行！");
            } else {
                new UpdateStudentView(mainView, selectedStudentIds[0]);
            }
        } else if (StringUtils.equals("删除", text)) {
            long[] selectedStudentIds = mainView.getSelectedStudentIds();
            if (ArrayUtils.isEmpty(selectedStudentIds)) {
                JOptionPane.showMessageDialog(mainView, "请选择要删除的数据！");
            } else {
                int option = JOptionPane.showConfirmDialog(mainView, "你确认要删除选择的" + selectedStudentIds.length + "行数据吗？", "确认删除？", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_NO_OPTION) {
                    // 确认
                    // 执行删除
                    StudentService studentService = new StudentServiceImpl();
                    boolean deleteResult = studentService.delete(selectedStudentIds);
                    if (deleteResult) {
                        mainView.reloadTable();
                    } else {
                        JOptionPane.showMessageDialog(mainView, "删除失败");
                    }

                }
            }
        } else if (StringUtils.equals("查询", text)) {
            mainView.setPageNo(1);
            mainView.reloadTable();
        } else if (StringUtils.equals("上一页", text)) {
            int pageNo = mainView.getPageNo() - 1;
            if (pageNo < 1) {
                pageNo = 1;
            }
            mainView.setPageNo(pageNo);
            mainView.reloadTable();
        } else if (StringUtils.equals("下一页", text)) {
            mainView.setPageNo(mainView.getPageNo() + 1);
            mainView.reloadTable();
        }
    }
}
